﻿using System;
using System.Data;
using System.Collections.Generic;
using Slja.Model;
using System.Text.RegularExpressions;
namespace Slja.BLL
{
	/// <summary>
	/// LinkMan
	/// </summary>
	public partial class LinkManBLL
	{
        private readonly Slja.DAL.LinkManDAL dal = new Slja.DAL.LinkManDAL();
        public LinkManBLL()
		{}
		#region  Method
        public string GetLinkManFromKeyWord(string keyword,LinkManType lmType)
        {
            string strXml = "";
            if (keyword.Length >= 2 || keyword.Length == 0)
            {
                Regex regChz2 = new Regex(@"[\u0391-\uFFE5]{2,}");//2个以上的汉字
                Regex regEng2 = new Regex(@"[A-Z,a-z]{2,}");//2个以上的拼音字母

                string strWhere = "[Type]='"+((int)lmType).ToString()+"'";
                if (regChz2.IsMatch(keyword))
                {
                    strWhere = " FullName like '" + keyword + "%'";
                }
                if (regEng2.IsMatch(keyword))
                {
                    strWhere = " Pinyin like '" + keyword + "%' OR ShortPy like '" + keyword + "%'";
                }

                //ClientDAL dal = new RegionCity();
                DataSet ds = dal.GetList(strWhere);
                if (ds.Tables.Count > 0)
                {
                    strXml = BLLCommon.BuildXmlData(ds.Tables[0], "ID", "FullName", "{0}*{1}", "{1}");
                }
            }
            return strXml;
        }
		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
			return dal.GetMaxId();
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ID)
		{
			return dal.Exists(ID);
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int  Add(Slja.Model.LinkManModel model)
		{
			return dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Slja.Model.LinkManModel model)
		{
			return dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			
			return dal.Delete(ID);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			return dal.DeleteList(IDlist );
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Slja.Model.LinkManModel GetModel(int ID)
		{
			
			return dal.GetModel(ID);
		}		

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			return dal.GetList(Top,strWhere,filedOrder);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Slja.Model.LinkManModel> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Slja.Model.LinkManModel> DataTableToList(DataTable dt)
		{
			List<Slja.Model.LinkManModel> modelList = new List<Slja.Model.LinkManModel>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				Slja.Model.LinkManModel model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = new Slja.Model.LinkManModel();
					if(dt.Rows[n]["ID"]!=null && dt.Rows[n]["ID"].ToString()!="")
					{
						model.ID=int.Parse(dt.Rows[n]["ID"].ToString());
					}
					if(dt.Rows[n]["FullName"]!=null && dt.Rows[n]["FullName"].ToString()!="")
					{
					model.FullName=dt.Rows[n]["FullName"].ToString();
					}
					if(dt.Rows[n]["Pingyin"]!=null && dt.Rows[n]["Pingyin"].ToString()!="")
					{
					model.Pinyin=dt.Rows[n]["Pingyin"].ToString();
					}
					if(dt.Rows[n]["ShortPY"]!=null && dt.Rows[n]["ShortPY"].ToString()!="")
					{
					model.ShortPY=dt.Rows[n]["ShortPY"].ToString();
					}
					if(dt.Rows[n]["Type"]!=null && dt.Rows[n]["Type"].ToString()!="")
					{
					model.Type=dt.Rows[n]["Type"].ToString();
					}
					if(dt.Rows[n]["Gender"]!=null && dt.Rows[n]["Gender"].ToString()!="")
					{
						model.Gender=int.Parse(dt.Rows[n]["Gender"].ToString());
					}
					if(dt.Rows[n]["Birthday"]!=null && dt.Rows[n]["Birthday"].ToString()!="")
					{
						model.Birthday=DateTime.Parse(dt.Rows[n]["Birthday"].ToString());
					}
					if(dt.Rows[n]["MobileTel"]!=null && dt.Rows[n]["MobileTel"].ToString()!="")
					{
					model.MobileTel=dt.Rows[n]["MobileTel"].ToString();
					}
					if(dt.Rows[n]["JobTel"]!=null && dt.Rows[n]["JobTel"].ToString()!="")
					{
					model.JobTel=dt.Rows[n]["JobTel"].ToString();
					}
					if(dt.Rows[n]["JobAddress"]!=null && dt.Rows[n]["JobAddress"].ToString()!="")
					{
					model.JobAddress=dt.Rows[n]["JobAddress"].ToString();
					}
					if(dt.Rows[n]["JobPosition"]!=null && dt.Rows[n]["JobPosition"].ToString()!="")
					{
					model.JobPosition=dt.Rows[n]["JobPosition"].ToString();
					}
					if(dt.Rows[n]["Email"]!=null && dt.Rows[n]["Email"].ToString()!="")
					{
					model.Email=dt.Rows[n]["Email"].ToString();
					}
					if(dt.Rows[n]["Company"]!=null && dt.Rows[n]["Company"].ToString()!="")
					{
					model.Company=dt.Rows[n]["Company"].ToString();
					}
					modelList.Add(model);
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			return dal.GetRecordCount(strWhere);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			return dal.GetListByPage( strWhere,  orderby,  startIndex,  endIndex);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		//{
			//return dal.GetList(PageSize,PageIndex,strWhere);
		//}

		#endregion  Method
	}
}

